package code

import (
	"fmt"
	"net/http"
)

type Code struct {
	HTTPStatus int
	Code       int
	Message    string
}

// WithError 拼接 error 内容到 message 中（用于调试）
func (c Code) WithError(err error) Code {
	if err == nil {
		return c
	}
	return Code{
		HTTPStatus: c.HTTPStatus,
		Code:       c.Code,
		Message:    fmt.Sprintf("%s: %s", c.Message, err.Error()),
	}
}

// WithInfo 拼接 字符串 内容到 message 中（用于调试）
func (c Code) WithInfo(info string) Code {
	if info == "" {
		return c
	}
	return Code{
		HTTPStatus: c.HTTPStatus,
		Code:       c.Code,
		Message:    fmt.Sprintf("%s: %s", c.Message, info),
	}
}

// 示例错误码（可按业务模块拆分）
var (
	Success       = Code{http.StatusOK, 0, "成功"}
	TokenExpired  = Code{http.StatusUnauthorized, 10001, "授权已过期"}
	TokenInvalid  = Code{http.StatusUnauthorized, 10002, "Token异常"}
	ParamError    = Code{http.StatusBadRequest, 10003, "参数错误"}
	ServerError   = Code{http.StatusInternalServerError, 10004, "服务器错误"}
	PermissionErr = Code{http.StatusForbidden, 10005, "权限不足"}
)
